package com.gpj.learn.rocketmq.simple;

import com.gpj.learn.rocketmq.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;

/**
 * @className SimpleProducer
 * @description
 *   简单的消息生产者,包含[发送同步消息],[发送异步消息]和[发送单向消息]
 * @author GPJ
 * @date 2021/4/22 21:43
 * @version 1.0
 **/
@Slf4j
public class SimpleProducer {
	public static void main(String[] args) throws InterruptedException {
		SimpleProducer producer = new SimpleProducer();
//		producer.syncSend();
//		producer.asyncSend();
		producer.oneway();
	}


	/**
	 * 同步发送消息:消息发送之后会阻塞等待消息发送的结果
	 */
	public void syncSend(){
		DefaultMQProducer producer = new DefaultMQProducer("SimpleProducerGroupName");
		producer.setNamesrvAddr(CommonConstants.ROCKETMQ_NAMESERVER_ADDR);
		try {
			producer.start();
			Message message = new Message("TestTopic", "Tag-A",
					"key:" + UUID.randomUUID().toString(),
					"this is a message".getBytes(RemotingHelper.DEFAULT_CHARSET));
			SendResult result = producer.send(message);
			log.info("消息发送结果{}", result.toString());
		} catch (UnsupportedEncodingException | MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
			e.printStackTrace();
		}finally {
			producer.shutdown();
		}
	}


	/**
	 * 发送异步消息:消息发送后,通过回调的方式接受返回信息
	 */
	public void asyncSend() throws InterruptedException {
		DefaultMQProducer producer = new DefaultMQProducer("SimpleProducerGroupName");
		producer.setNamesrvAddr(CommonConstants.ROCKETMQ_NAMESERVER_ADDR);
		CountDownLatch countDownLatch = new CountDownLatch(1);
		try {
			producer.start();
			producer.setRetryTimesWhenSendAsyncFailed(0);

			Message message = new Message("TestTopic", "Tag-A",
					"key:" + UUID.randomUUID().toString(),
					"this is a message".getBytes(RemotingHelper.DEFAULT_CHARSET));
			producer.send(message, new SendCallback() {
				@Override
				public void onSuccess(SendResult result) {
					log.info("消息发送成功:{}", result);
					countDownLatch.countDown();
				}

				@Override
				public void onException(Throwable throwable) {
					log.error("消息发送失败:{}", throwable.getCause(), throwable);
					countDownLatch.countDown();
				}
			});
		} catch (MQClientException | UnsupportedEncodingException | RemotingException | InterruptedException e) {
			e.printStackTrace();
		}finally {
			countDownLatch.await();
			producer.shutdown();
		}
	}

	/**
	 * 发送单向消息:即只负责发送消息,不接受返回参数,不确认消息是否发送成功
	 */
	public void oneway(){
		DefaultMQProducer producer = new DefaultMQProducer("SimpleProducerGroupName");
		producer.setNamesrvAddr(CommonConstants.ROCKETMQ_NAMESERVER_ADDR);
		try {
			producer.start();
			Message message = new Message("TestTopic", "Tag-A",
					"key:" + UUID.randomUUID().toString(),
					"this is a message".getBytes(RemotingHelper.DEFAULT_CHARSET));
			producer.sendOneway(message);
			log.info("消息已发送");
		} catch (UnsupportedEncodingException | MQClientException | RemotingException | InterruptedException e) {
			e.printStackTrace();
		}finally {
			producer.shutdown();
		}
	}
}
